using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.AI;

public class NPCFollow : MonoBehaviour
{
    private NavMeshAgent navMeshAgent;
    private Transform player;

    public float followDistance = 5f;
    public bool following = false;
    private Matching _matching;
    public int id;
    public bool matched = false;

    void Start()
    {
        navMeshAgent = GetComponent<NavMeshAgent>();
        player = GameObject.FindGameObjectWithTag("Player").transform; // Assuming the player has the tag "Player"
        _matching = player.GetComponent<Matching>();
    }

    void Update()
    {
        if (IsPlayerInRange())
        {
            // Follow the player if they are within the specified follow distance
            navMeshAgent.SetDestination(PlayerPosition());
            if (!following)
            {
                _matching.subscribeFollow(id-1);
            }
            following = true;
        }
        else
        {
            following = false;
            _matching.unsubscribeFollow(id-1);
        }
    }

    bool IsPlayerInRange()
    {
        // Check if the player is within the specified follow distance
        float distanceToPlayer = Vector3.Distance(transform.position, PlayerPosition());
        return distanceToPlayer <= followDistance;
    }

    Vector3 PlayerPosition()
    {
        // Return the player's position
        return player.position;
    }
}